# coding=utf-8
# 代码文件：chapter8/ch8.1.6.py
from docx.shared import Cm

from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE

SLD_LAYOUT_TITLE_ONLY = 5  # 只有标题的版式

from pptx import Presentation

f = r'data/temp.pptx'
# 打开PPT文档
ppt = Presentation(f)

# 选择母版中的第六个幻灯片版式，只有标题的版式
title_slide_layout = ppt.slide_layouts[SLD_LAYOUT_TITLE_ONLY]

print('添加一页幻灯片。')
#  添加一页幻灯片
slide = ppt.slides.add_slide(title_slide_layout)

title = slide.shapes.title         # 获得幻灯片的标题

title.text = '北京各城区最高房价'     # 设置标题文本

chart_data = CategoryChartData()

# 设置x轴数据
chart_data.categories = ['西城', '海淀', '朝阳', '丰台', '东城']
# 设置y轴数据
chart_data.add_series('平均价', (201991, 129508, 130969, 119170, 79933))

# 获得图表对象
shape = slide.shapes.add_chart(chart_type=XL_CHART_TYPE.COLUMN_CLUSTERED,
                               x=Cm(3.89),
                               y=Cm(5.21),
                               cx=Cm(17.63),
                               cy=Cm(10.66),
                               chart_data=chart_data)
agechart = shape.chart

x_axis = agechart.category_axis       # x轴对象

x_axis_title = x_axis.axis_title      # x轴标题

x_axis_title.text_frame.text = '城区'  # 设置x轴标题

y_axis = agechart.value_axis        # y轴对象

y_axis_title = y_axis.axis_title   # y轴标题

y_axis_title.text_frame.text = '单价（元）'  # 设置y轴标题

# 保存文件
ppt.save(f)

print("Game Over")
